package greate.project.mysql;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.fastjson.JSONObject;
import org.apache.camel.CamelContext;
import org.apache.camel.Exchange;
import org.apache.camel.Processor;
import org.apache.camel.builder.RouteBuilder;
import org.apache.camel.impl.DefaultCamelContext;

import java.util.LinkedHashMap;
import java.util.UUID;

/**
 * @author v_zhangshun01
 */
public class MySqlApp02 {

    public static void main(String[] args) throws Exception {
        // 创建默认上下文
        CamelContext context = new DefaultCamelContext();

        // 现开启想表达的是 ， camel 在开启后可以动态的新增和关闭路由。
        context.start();

        context.getRegistry().bind("druidDataSource" , dataSource());

        context.addRoutes(new MySqlRoute());

        synchronized (MySqlApp02.class) {
            MySqlApp02.class.wait();
        }

    }


    static class MySqlRoute extends RouteBuilder {

        @Override
        public void configure() throws Exception {

            // 只执行一次
            from("timer:foo?repeatCount=1")
                    // world 一下。
                    .log("hello world.")
                    .setBody(constant("SELECT * FROM acg_datasource"))
                    .to("jdbc:druidDataSource?outputType=SelectList")
                    .split(body())
                    .choice()
                    .when(jsonpath("$.remark").isEqualTo("1"))
                    .setBody(constant(
                            "INSERT INTO `acg_datasource` " +
                                    "(`id`, `name`, `remark`, `status`, `type`, `info_id`, `create_by`, `create_time`, `update_by`, `update_time`) " +
                                    "VALUES " +
                                    "('"+ UUID.randomUUID() +"', '${body[name]}', '${body[remark]}', NULL, '1', '1409759561458253826', NULL, '2021-06-29 14:24:06', NULL, NULL);"))
                    .to("jdbc:druidDataSource")
                    .endChoice();


        }
    }

    static DruidDataSource dataSource(){
        DruidDataSource dataSource = new DruidDataSource();
        dataSource.setUrl("jdbc:mysql://120.48.5.4:8806/data-exchange-zs?useSSL=false");
        dataSource.setUsername("root");
        dataSource.setPassword("baidu@123");
        dataSource.setDriverClassName("com.mysql.cj.jdbc.Driver");
        return dataSource;
    }

}
